package com.usermanagement.controller;

import com.cac409club.usermanagement.dto.*;
import com.cac409club.usermanagement.vo.RestResult;
import com.usermanagement.constant.ResultCodeConstant;
import com.usermanagement.dto.AssignPermissionDTO;
import com.usermanagement.dto.LoginDTO;
import com.usermanagement.dto.ResetPasswordDTO;
import com.usermanagement.dto.UserDTO;
import com.usermanagement.entity.UserDO;
import com.usermanagement.exception.BusinessException;
import com.usermanagement.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户管理")
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userDTO 用户信息
     * @return 注册结果
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public RestResult<Boolean> registerUser(@RequestBody @Validated UserDTO userDTO) {
        try {
            Boolean result = userService.registerUser(userDTO);
            return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
        } catch (BusinessException e) {
            return new RestResult<>(e.getCode(), e.getMsg());
        }
    }

    /**
     * 用户登录
     *
     * @param loginDTO 登录信息
     * @return 用户信息
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public RestResult<UserDO> loginUser(@RequestBody @Validated LoginDTO loginDTO) {
        try {
            UserDO result = userService.loginUser(loginDTO);
            return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
        } catch (BusinessException e) {
            return new RestResult<>(e.getCode(), e.getMsg());
        }
    }

    /**
     * 密码重置
     *
     * @param resetPasswordDTO 重置密码信息
     * @return 重置结果
     */
    @PostMapping("/reset-password")
    @ApiOperation("密码重置")
    public RestResult<Boolean> resetPassword(@RequestBody @Validated ResetPasswordDTO resetPasswordDTO) {
        try {
            Boolean result = userService.resetPassword(resetPasswordDTO);
            return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
        } catch (BusinessException e) {
            return new RestResult<>(e.getCode(), e.getMsg());
        }
    }

    /**
     * 分配用户权限
     *
     * @param assignPermissionDTO 权限分配信息
     * @return 分配结果
     */
    @PostMapping("/assign-permissions")
    @ApiOperation("分配用户权限")
    public RestResult<Boolean> assignPermissions(@RequestBody @Validated AssignPermissionDTO assignPermissionDTO) {
        try {
            Boolean result = userService.assignPermissions(assignPermissionDTO);
            return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
        } catch (BusinessException e) {
            return new RestResult<>(e.getCode(), e.getMsg());
        }
    }
}